package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;

import appbase.jfinal.ext.ApiBaseController;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Account;
import dshop.model.AppSuperman;

/**
 * App管理者账号
 */
@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class SupermanAction extends ApiBaseController{
	

	public void list()
	{
		String keyWord = getPara("keyWord", "").trim();
		
		String select = " select s.id, s.account_id, m.phone, m.gender, concat(m.last_name, m.first_name) as realName, m.avatar ";
		String where = " from app_superman s "
					 + " inner join member_general_info m on m.account_id = s.account_id where 1 = 1 ";
		String orderBy = " order by s.id desc ";
		List<Object> params = new ArrayList<>();
		if (!keyWord.isEmpty())
		{
			where += " and m.phone = ? ";
			params.add(keyWord);
		}
		
		page(select, where + orderBy, params.toArray());
		renderSuccess();
	}
	
	
	public void add()
	{
		String phone = getPara("phone");
		
		Account account = Account.dao.findFirstByColumns("id", "phone", phone);
		if (account == null)
		{
			renderFail("手机号不存在");
			return;
		}
		if (AppSuperman.isExisted(account.getInt("id")))
		{
			renderFail("不能重复添加");
			return;
		}
		
		AppSuperman.createAndSave(account.getInt("id"));
		
		renderSuccess("添加成功");
	}
	
	
	public void delete()
	{
		int supermanId = getParaToInt("id");
		
		AppSuperman.deleteBy(supermanId);
		
		renderSuccess("移除成功");
	}
	
	
	
}
